*------------------------------------------------------------------------------*
* Bunching Analysis *
*------------------------------------------------------------------------------*

*--- London: All sales of new builds only
use "${datadir}\HTB_main_1m", clear

// 0-800
hist purchase_price if regionname == "London" & purchaseprice < 800 * 1000 ///
	& year < 2018, ///
	graphregion(color(white)) note("") ///
	freq xlabel(0 600000, format(%7.0fc)) ///
	xline(600000, lpattern(dash)) xti("Purchase price") ///
	ylabel(, angle(0)) ///
	width(25000) disc 
	
gr export "${figuredir}\bunching_allnew_London_psd.pdf", ///
	as(pdf) replace	
	
	
*--- Main	
use "${projectdir}\\data\HTB_main_1m", replace

keep if inrange(purchaseprice, 500, 700)
keep if region==5

gen purchaseprice20 = round(purchaseprice,20)

gen el = equity / purchaseprice

gen ftb = advtype==3 


*age
rdplot age purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Age", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(, angle(0)) ///
		graphregion(color(white)) name(gr1, replace) ) //

* ftb
rdplot ftb purchaseprice , c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("First-time-buyers", size(large)) xtitle("Purchase Price", size(large))  ///
		ylabel(0.25(0.20)0.85) ylabel(, angle(0)) ///
		graphregion(color(white)) name(gr2, replace) ) 
		
gr combine gr1 gr2, graphregion(color(white)) xsize(8) 
gr export "${figuredir}\bunching_age_ftb_allyear.pdf", ///
	as(pdf) replace


* down payment
rdplot deposit_final purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Down Payment", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(, angle(0)) ///
		graphregion(color(white)) name(gr1, replace) ) //

* income
rdplot gross_income purchaseprice , c(601) p(1) nbins(5 5) ci(95) ///
		graph_options( ///
		ytitle("Income (Gross)", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(80(20)160) ylabel(, angle(0)) ///
		graphregion(color(white)) name(gr2, replace) ) //		
		
gr combine gr1 gr2, graphregion(color(white))  xsize(8) 
gr export "${figuredir}\bunching_deposit_income_allyear.pdf", ///
	as(pdf) replace	
	
	

* LTV-CLTV
rdplot  LTV  purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Loan-to-Value", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(50(10)80) ///
		graphregion(color(white)) name(gr5, replace) ) //

rdplot  combined_LTV purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Combined Loan-to-Value", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(50(10)80) ///
		graphregion(color(white)) name(gr6, replace) ) //
				
gr combine gr5 gr6, graphregion(color(white)) xsize(8)
gr export "${figuredir}\bunching_ltv_cltv_allyear.pdf", ///
	as(pdf) replace

*  LTI-CLTI
rdplot  LTI purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Loan-to-Income", size(large)) xtitle("Purchase Price", size(large)) ///
		 ///
		graphregion(color(white)) name(gr7, replace) ) //

rdplot  combined_LTI purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Combined Loan-to-Income", size(large)) xtitle("Purchase Price", size(large)) ///
		 ///
		graphregion(color(white)) name(gr8, replace) ) //
			
gr combine gr7 gr8, graphregion(color(white)) ycommon xsize(8) 
gr export "${figuredir}\bunching_lti_clti_allyear.pdf", ///
	as(pdf) replace


	* Maturity
rdplot  mortgage_term purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Maturity", size(large)) xtitle("Purchase Price", size(large)) ///
		graphregion(color(white)) name(gr11, replace) ) //
gr export "${figuredir}\bunching_maturity_allyear.pdf", ///
	as(pdf) replace
	
	
*--- Placebo 2009-2012
use "${datadir}\PSD001_new_2009-2012_London", replace

gen purchaseprice = purchase_price/1000
keep if inrange(purchaseprice, 500, 700)

gen purchaseprice20 = round(purchaseprice,20)

rdplot age purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Age") xtitle("Purchase Price") ///
		ylabel(33(1)40) ///
		graphregion(color(white)) name(gr1, replace) ) //
		
gen ftb = advtype==3 
rdplot ftb purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("First-time-buyers") xtitle("Purchase Price") ///
		ylabel(0.15(0.20)0.80) ///
		graphregion(color(white)) name(gr2, replace) ) //
		
gr combine gr1 gr2, graphregion(color(white)) xsize(8) 
gr export "${figuredir}\bunching_age_ftb_placebo_allyear.pdf", ///
	as(pdf) replace


gen deposit_final 	= purchaseprice - loan_val/1000
rdplot deposit_final purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Down Payment") xtitle("Purchase Price") ///
		ylabel(140(20)310) ///
		graphregion(color(white)) name(gr3, replace) ) //

replace  gross_income =  gross_income/1000
rdplot  gross_income purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// 
		graph_options( ///
		ytitle("Income (Gross)") xtitle("Purchase Price") ///
		ylabel(80(20)180) ///
		graphregion(color(white)) name(gr4, replace) ) //
		
gr combine gr3 gr4, graphregion(color(white)) xsize(8) 
gr export "${figuredir}\bunching_deposit_income_placebo_allyear.pdf", ///
	as(pdf) replace


	
*--- Placebo old: RD charts
use "${projectdir}\\data\HTB_main_bunching600old", replace

replace purchaseprice = purchaseprice/1000
keep if inrange(purchaseprice, 500, 700)
*keep if year>2015 // 2014
keep if region==5

gen purchaseprice20 = round(purchaseprice,20)

rdplot age purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// shade
		graph_options( ///
		ylabel(33(1)40) ///
		ytitle("Age", size(large)) xtitle("Purchase Price", size(large)) ///
		graphregion(color(white)) name(gr1, replace) ) //
		

gen ftb = advtype==3 
rdplot ftb purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// shade
		graph_options( ///
		ytitle("First-time-buyers", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(0.15(0.20)0.80) ///
		graphregion(color(white)) name(gr2, replace) ) //
		
gr combine gr1 gr2, graphregion(color(white)) xsize(8) //scale(1.4)
gr export "${figuredir}\bunching_age_ftb_placebo_old_allyear.pdf", ///
	as(pdf) replace
*graph drop _all


gen deposit_final 	= purchaseprice - loan_val/1000
rdplot deposit_final purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// shade
		graph_options( ///
		ytitle("Down Payment", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(140(20)310) ///
		graphregion(color(white)) name(gr3, replace) ) //

replace  gross_income =  gross_income/1000
rdplot  gross_income purchaseprice, c(601) p(1) nbins(5 5) ci(95) /// shade
		graph_options( ///
		ytitle("Income (Gross)", size(large)) xtitle("Purchase Price", size(large)) ///
		ylabel(80(20)180) ///
		graphregion(color(white)) name(gr4, replace) ) //
		
gr combine gr3 gr4, graphregion(color(white)) xsize(8) //scale(1.4)
gr export "${figuredir}\bunching_deposit_income_placebo_old_allyear.pdf", ///
	as(pdf) replace
	



*--- Placebo old: bunching histogram
use "${projectdir}\\data\HTB_main_bunching600old", replace
keep if region==5		// London

	// 0-800
hist purchase_price if purchaseprice < 800 * 1000 ///
	& year < 2018, ///
	graphregion(color(white)) note("") ///
	freq xlabel(0 600000, format(%7.0fc)) ///
	xline(600000, lpattern(dash)) xti("Purchase price") ///
	ylabel(, angle(0) format(%7.0f)) ///
	width(25000) disc ti("Existing houses, 2013-2017") ///
	name(gr1, replace)

	
*--- Placebo new 2009-2012: bunching histogram
use "${datadir}\PSD001_new_2009-2012_London", replace


	// 0-800
hist purchase_price if purchaseprice < 800 * 1000 ///
	& year < 2018, ///
	graphregion(color(white)) note("") ///
	freq xlabel(0 600000, format(%7.0fc)) ///
	xline(600000, lpattern(dash)) xti("Purchase price") ///
	ylabel(, angle(0) format(%7.0f)) ///
	width(25000) disc ti("New houses, 2009-2012") ///
	name(gr2, replace)

gr combine gr1 gr2, graphregion(color(white)) xsize(9) ycommon
gr export "${figuredir}\bunching_placebo_London_psd.pdf", ///
	as(pdf) replace	
